import com.itheima.domain.User;
import com.itheima.factory.UserServiceFactory;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class UserApp {
    @Test
    public void test1() {
        //通过加载spring的类配置文件来获得ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //通过ioc容器来获得对象      使用id + 接口的class类对象的方式
        UserService user = context.getBean("userService", UserService.class);

        System.out.println(user);

        user.say();

        //通过加载spring的类配置文件来获得ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        //通过ioc容器来获得对象    使用name +  实现类的class对象方式
        UserService userService = ctx.getBean("service", UserServiceImpl.class);

        userService.say();
    }

    @Test
    public void test2() {
        //通过加载spring的类配置文件来获得ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        //通过ioc容器来获得对象      使用id + 接口的实现类对象的方式
        UserServiceImpl u2 = ctx.getBean("u2", UserServiceImpl.class);

//        u2.init();
        u2.destroy();
    }


    @Test
    public void test3() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        UserServiceImpl u3 = (UserServiceImpl) ctx.getBean("u3");

        u3.say();

    }

    @Test
    //使用实例工厂来创建bean对象
    public void test4() {
        //通过加载配置文件来获得ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        //通过实例工厂来创建对象
        UserServiceImpl u4 = ctx.getBean("u4", UserServiceImpl.class);
        //调用方法
        u4.say();
    }

    @Test
    public void test5() {
        //通过加载配置文件获取ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        //通过实例工厂来创建对象
        User user = ctx.getBean("user", User.class);

        //获取user对象中的属性
        System.out.println("age----" + user.getAge());
        System.out.println("name----" + user.getName());
        System.out.println("birthday-----" + user.getBirthday());
        System.out.println("userService-----" + user.getUserService());
        user.getUserService().say();
    }

    @Test
    public void test6() {
        //通过加载配置文件获取ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        //通过实例工厂来创建对象
        User user = ctx.getBean("user", User.class);

        //获取user对象中的属性
        System.out.println(user.getAge());
        System.out.println(user.getName());

        System.out.println(user.getBirthday());

        System.out.println(user.getUserService());
    }

    @Test
    public void test7(){
        //通过加载配置文件获取ioc容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

        //通过实例工厂来创建对象
        User user = ctx.getBean("user", User.class);

        //获取对象属性值
        System.out.println(user.getList());
        System.out.println(user.getProperties());
        System.out.println(Arrays.toString(user.getArr()));
        System.out.println(user.getHs());
        System.out.println(user.getHm());
    }
}
